package hammersmith.local.ui.gui;

import hammersmith.local.exceptions.ArticleNotFoundException;
import hammersmith.local.management.ShopManagement;
import hammersmith.local.valueobjects.Article;
import hammersmith.local.valueobjects.Employee;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class EditArticleMask extends JFrame {
	private JTextField txtArtNr;
	private JTextField txtType;
	private JTextField txtName;
	private JTextField txtSize;
	private JTextField txtColor;
	private JTextField txtStock;
	private JTextField txtPrice;
	private JTextField txtMinCount;
	Article article = null;

	public EditArticleMask(final ShopManagement shop, String id, final Employee e) {

		try {
			article = shop.getArticleManagement().getArticleById(
					Long.valueOf(id));
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
		} catch (ArticleNotFoundException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
		}

		setMinimumSize(new Dimension(500, 500));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 120, 79, 139, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0, 1.0, 1.0,
				0.0, 0.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		JLabel lblTitel = new JLabel("Artikel ");
		lblTitel.setFont(new Font("Dialog", Font.BOLD, 20));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.gridwidth = 7;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		getContentPane().add(lblTitel, gbc_lblNewLabel);

		JLabel lblArtnr = new JLabel("Art.-Nr.");
		GridBagConstraints gbc_lblArtnr = new GridBagConstraints();
		gbc_lblArtnr.anchor = GridBagConstraints.EAST;
		gbc_lblArtnr.insets = new Insets(0, 0, 5, 5);
		gbc_lblArtnr.gridx = 1;
		gbc_lblArtnr.gridy = 2;
		getContentPane().add(lblArtnr, gbc_lblArtnr);

		txtArtNr = new JTextField(String.valueOf(article.getId()));
		txtArtNr.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtArtNr.setEnabled(false);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 2;
		gbc_textField.gridy = 2;
		getContentPane().add(txtArtNr, gbc_textField);
		txtArtNr.setColumns(1);

		JLabel lblFarbe = new JLabel("Farbe");
		GridBagConstraints gbc_lblFarbe = new GridBagConstraints();
		gbc_lblFarbe.anchor = GridBagConstraints.EAST;
		gbc_lblFarbe.insets = new Insets(0, 0, 5, 5);
		gbc_lblFarbe.gridx = 3;
		gbc_lblFarbe.gridy = 2;
		getContentPane().add(lblFarbe, gbc_lblFarbe);

		txtColor = new JTextField(article.getColor());
		txtColor.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_textField_4 = new GridBagConstraints();
		gbc_textField_4.insets = new Insets(0, 0, 5, 5);
		gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_4.gridx = 4;
		gbc_textField_4.gridy = 2;
		getContentPane().add(txtColor, gbc_textField_4);
		txtColor.setColumns(10);

		JLabel lblTyp = new JLabel("Typ");
		GridBagConstraints gbc_lblTyp = new GridBagConstraints();
		gbc_lblTyp.anchor = GridBagConstraints.EAST;
		gbc_lblTyp.insets = new Insets(0, 0, 5, 5);
		gbc_lblTyp.gridx = 1;
		gbc_lblTyp.gridy = 4;
		getContentPane().add(lblTyp, gbc_lblTyp);

		txtType = new JTextField(article.getType());
		txtType.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.insets = new Insets(0, 0, 5, 5);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 2;
		gbc_textField_1.gridy = 4;
		getContentPane().add(txtType, gbc_textField_1);
		txtType.setColumns(10);

		JLabel lblBestand = new JLabel("Bestand");
		GridBagConstraints gbc_lblBestand = new GridBagConstraints();
		gbc_lblBestand.anchor = GridBagConstraints.EAST;
		gbc_lblBestand.insets = new Insets(0, 0, 5, 5);
		gbc_lblBestand.gridx = 3;
		gbc_lblBestand.gridy = 4;
		getContentPane().add(lblBestand, gbc_lblBestand);

		txtStock = new JTextField(String.valueOf(article.getStock()));
		txtStock.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtStock.setColumns(10);
		GridBagConstraints gbc_textField_5 = new GridBagConstraints();
		gbc_textField_5.insets = new Insets(0, 0, 5, 5);
		gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_5.gridx = 4;
		gbc_textField_5.gridy = 4;
		getContentPane().add(txtStock, gbc_textField_5);

		JLabel lblName = new JLabel("Name");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 6;
		getContentPane().add(lblName, gbc_lblName);

		txtName = new JTextField(article.getName());
		txtName.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtName.setColumns(10);
		GridBagConstraints gbc_textField_2 = new GridBagConstraints();
		gbc_textField_2.insets = new Insets(0, 0, 5, 5);
		gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_2.gridx = 2;
		gbc_textField_2.gridy = 6;
		getContentPane().add(txtName, gbc_textField_2);

		JLabel lblPreis = new JLabel("Preis");
		GridBagConstraints gbc_lblPreis = new GridBagConstraints();
		gbc_lblPreis.anchor = GridBagConstraints.EAST;
		gbc_lblPreis.insets = new Insets(0, 0, 5, 5);
		gbc_lblPreis.gridx = 3;
		gbc_lblPreis.gridy = 6;
		getContentPane().add(lblPreis, gbc_lblPreis);

		txtPrice = new JTextField(String.valueOf(article.getPrice()));
		txtPrice.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtPrice.setColumns(10);
		GridBagConstraints gbc_textField_6 = new GridBagConstraints();
		gbc_textField_6.insets = new Insets(0, 0, 5, 5);
		gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_6.gridx = 4;
		gbc_textField_6.gridy = 6;
		getContentPane().add(txtPrice, gbc_textField_6);

		JLabel lblGre = new JLabel("Gr\u00F6\u00DFe");
		GridBagConstraints gbc_lblGre = new GridBagConstraints();
		gbc_lblGre.anchor = GridBagConstraints.EAST;
		gbc_lblGre.insets = new Insets(0, 0, 5, 5);
		gbc_lblGre.gridx = 1;
		gbc_lblGre.gridy = 8;
		getContentPane().add(lblGre, gbc_lblGre);

		txtSize = new JTextField(article.getSize());
		txtSize.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtSize.setColumns(10);
		GridBagConstraints gbc_textField_3 = new GridBagConstraints();
		gbc_textField_3.insets = new Insets(0, 0, 5, 5);
		gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_3.gridx = 2;
		gbc_textField_3.gridy = 8;
		getContentPane().add(txtSize, gbc_textField_3);

		JLabel lblLosgre = new JLabel("Losgr\u00F6\u00DFe");
		GridBagConstraints gbc_lblLosgre = new GridBagConstraints();
		gbc_lblLosgre.anchor = GridBagConstraints.EAST;
		gbc_lblLosgre.insets = new Insets(0, 0, 5, 5);
		gbc_lblLosgre.gridx = 3;
		gbc_lblLosgre.gridy = 8;
		getContentPane().add(lblLosgre, gbc_lblLosgre);

		txtMinCount = new JTextField("1");
		txtMinCount.setBorder(new LineBorder(new Color(0, 0, 0)));
		txtMinCount.setColumns(10);
		GridBagConstraints gbc_textField_7 = new GridBagConstraints();
		gbc_textField_7.insets = new Insets(0, 0, 5, 5);
		gbc_textField_7.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_7.gridx = 4;
		gbc_textField_7.gridy = 8;
		getContentPane().add(txtMinCount, gbc_textField_7);

		JLabel lblBeschreibung = new JLabel("Beschreibung");
		GridBagConstraints gbc_lblBeschreibung = new GridBagConstraints();
		gbc_lblBeschreibung.insets = new Insets(0, 0, 5, 5);
		gbc_lblBeschreibung.gridx = 1;
		gbc_lblBeschreibung.gridy = 10;
		getContentPane().add(lblBeschreibung, gbc_lblBeschreibung);

		final JTextArea txtDescription = new JTextArea(article.getDescription());
		txtDescription.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 3;
		gbc_textArea.insets = new Insets(0, 0, 5, 5);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 2;
		gbc_textArea.gridy = 10;
		getContentPane().add(txtDescription, gbc_textArea);

		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shop.editArticles(e, article, txtType.getText(), txtName.getText(), txtSize.getText(), txtColor.getText(), txtStock.getText(), txtPrice.getText(), txtMinCount.getText(), txtDescription.getText());
				dispose();
				shop.saveDB();
			}
		});
		btnOk.setActionCommand("");
		GridBagConstraints gbc_btnBesttigen = new GridBagConstraints();
		gbc_btnBesttigen.insets = new Insets(0, 0, 5, 5);
		gbc_btnBesttigen.gridx = 3;
		gbc_btnBesttigen.gridy = 12;
		getContentPane().add(btnOk, gbc_btnBesttigen);

		JButton btnAbbrechen = new JButton("Abbrechen");
		GridBagConstraints gbc_btnAbbrechen = new GridBagConstraints();
		gbc_btnAbbrechen.insets = new Insets(0, 0, 5, 5);
		gbc_btnAbbrechen.gridx = 4;
		gbc_btnAbbrechen.gridy = 12;
		getContentPane().add(btnAbbrechen, gbc_btnAbbrechen);

		this.setLocationRelativeTo(null);
		this.setVisible(true);

	}
}
